package uk.co.richardwkirk.parkrun.data.events;

import java.io.PrintStream;
import java.util.Collection;

import uk.co.richardwkirk.parkrun.geo.Location;
import uk.co.richardwkirk.parkrun.geo.Region;
import uk.co.richardwkirk.parkrun.news.NewsSubject;
import uk.co.richardwkirk.parkrun.web.ParkrunURLGenerator;
import uk.co.richardwkirk.parkrun.web.ParkrunURLGenerator.ParkrunWebPages;

public class Event implements NewsSubject {

	String shortName;
	String fullName;
	int id;
	int c; // No clue what this is for but lets store it anyway
	Location location;
	int regionId;
	Region region;

	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public int getRegionId() {
		return regionId;
	}

	public void setRegionId(int region) {
		this.regionId = region;
	}
	
	public Region getRegion() {
		return region;
	}

	public void setRegion(Region region) {
		this.region = region;
	}

	public static void print(Collection<Event> allEvents, PrintStream out) {
		for (Event event : allEvents) {
			out.print(event.fullName);
			out.print(" (");
			out.print(event.shortName);
			out.print(")");
			out.println("");
		}
	}

	public String getRegionString() {
		return getRegionString(region);
	}
	
	private String getRegionString(Region region) {
		StringBuilder regionString = new StringBuilder();
		Region parentRegion = region.getParentRegion();
		if (parentRegion != null) {
			regionString.append(getRegionString(parentRegion));
			regionString.append(" > ");
		}
		regionString.append(region.getName());
		return regionString.toString();
	}

	public String getEventURL(ParkrunWebPages pageType) {
		String siteURL = region.getParkrunSiteURL();
		ParkrunURLGenerator urlGenerator = new ParkrunURLGenerator(siteURL);
		return urlGenerator.createEventURL(shortName, pageType);
	}
	
	/**
	 * NEWS FUNCTIONS FOLLOW
	 */
	@Override
	public String getSubjectName() {
		return this.getFullName();
	}

	@Override
	public String getNewsFeedUrl() {
		return getEventURL(ParkrunURLGenerator.ParkrunWebPages.NewsFeed);
	}

}
